/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit.persistance;

import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.sql.*;
import java.util.Calendar;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;

/**
 *
 * @author 시리우스
 */
public class ChangeUnitDAO extends AbstractChangeUnitDAO {

    @Override
    public ChangeUnitDTO find(String changeUnitId) throws PersistanceException {
        ChangeUnitDTO dto = null;
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select CHANGEUNITID, LOG, DATE, TIME, USERID "
                + " from APP.CHANGEUNIT where CHANGEUNITID = '"+changeUnitId+"'";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            if(rs.next()) {
                dto = new ChangeUnitDTO();
                
                dto.setChangeUnitId(rs.getString("CHANGEUNITID"));
                dto.setLog(rs.getString("LOG"));
                dto.setDate(rs.getDate("DATE"));
                dto.setTime(rs.getTime("TIME")); 
                dto.setUserId(rs.getString("USERID"));
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing ChangeUnitDAO.find: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dto; 
    }

    @Override
    public List<ChangeUnitDTO> list() throws PersistanceException {
        
        List<ChangeUnitDTO> dtoArray = new ArrayList<ChangeUnitDTO>();
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select CHANGEUNITID, LOG, DATE, TIME, USERID  from APP.CHANGEUNIT";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            while(rs.next()){
                ChangeUnitDTO dto = new ChangeUnitDTO();
                dto.setChangeUnitId(rs.getString("CHANGEUNITID"));
                dto.setLog(rs.getString("LOG"));
                dto.setDate(rs.getDate("DATE"));
                dto.setTime(rs.getTime("TIME"));                
                dto.setUserId(rs.getString("USERID"));
              
                dtoArray.add(dto);
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing ChangeUnitDAO.list: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dtoArray; 
    }

    @Override
    public void insert(ChangeUnitDTO changeUnitDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
        // 승민 - 임시로 0, 0 값을 넣게 해놨습니당 ^^ 나중에 수정해주세요
        //String sql="insert into APP.CHANGEUNIT (CHANGEUNITID, LOG, DATE, TIME, USERID) " 
        //            + "values ('"+changeUnitDTO.getChangeUnitId()+"','"+changeUnitDTO.getLog()+"','" + new Date() + "','" + new Time(System.currentTimeMillis()) +"','"+changeUnitDTO.getUserId()+"')";
        String sql="insert into APP.CHANGEUNIT (CHANGEUNITID, LOG, DATE, TIME, USERID) " 
                    + "values (?, ?, ?, ?, ?)";
        
        System.out.println(sql);
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, changeUnitDTO.getChangeUnitId());
            stmt.setString(2, changeUnitDTO.getLog());
            stmt.setDate(3, new java.sql.Date(System.currentTimeMillis()));
            stmt.setTime(4, new Time(System.currentTimeMillis()));
            stmt.setString(5, changeUnitDTO.getUserId());
            
            stmt.executeUpdate();
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ChangeUnitDAO.insert: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }
    }

    @Override
    public void update(ChangeUnitDTO changeUnitDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
        String sql="update APP.CHANGEUNIT set CHANGEUNITID=?,LOG=?,DATE= ?,TIME=?,USERID=? where CHANGEUNITID =?";
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, changeUnitDTO.getChangeUnitId());
            stmt.setString(2, changeUnitDTO.getLog());
            stmt.setDate(3, new java.sql.Date(System.currentTimeMillis()));
            stmt.setTime(4, new Time(System.currentTimeMillis()));
            stmt.setString(5, changeUnitDTO.getUserId());
            stmt.setString(6, changeUnitDTO.getChangeUnitId());
            
            stmt.executeUpdate();
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ChangeUnitDAO.update: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }  
    }

    @Override
    public void delete(String changeUnitId) throws PersistanceException {
        Connection conn = null; 
        Statement stmt = null;  
        
        //Create SQL Statement 
        String sql="delete from APP.CHANGEUNIT "  
                   +"WHERE CHANGEUNITID = '"+changeUnitId+"'";  
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            stmt.executeUpdate(sql); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ChangeUnitDAO.delete: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
    }

}
